/*axios拦截*/
import Vue from 'vue'
import axios from 'axios'
import router from '../router'
import store from '../store'

// axios.defaults.timeout = 30000;
// axios.defaults.retry = 4; //请求超时次数    
// axios.defaults.retryDelay = 30000; //重新请求间隔
const vm = new Vue();
// 添加请求拦截器
axios.interceptors.request.use(config => {
    //判断是否存在token，如果存在将每个页面header都添加token
    
    var token = localStorage.getItem("token");
    if (token) {
        config.headers.common['token'] = token;
    }
    if(config.btn){
        if(config.btn.classList.contains('com_loading')==true){
            return new Promise(() => {});//取消请求
        }else{
            config.btn.classList.add("com_loading");
        }
      
    }

    return config;
}, error => {
    // Mint.Indicator.close();
    // 对请求错误做些什么
    return Promise.reject(error);
});
axios.interceptors.response.use(response => {
    switch (response.data.code) {
            case 8888:
            vm.$Message.error(response.data.msg);
            if(location.hash== '#/trade' || location.hash== '#/stock' ){
                store.commit('cleartloding');
             }else{
                store.commit('cleartloding', '/index');
             }
            break;
    }
   
    if(response.config.btn){
        setTimeout(()=>{
            response.config.btn.classList.remove("com_loading");
        },200)
      
    }
   
    return response;
}, function axiosRetryInterceptor(err) {
    if(err.config&&err.config.btn){
        setTimeout(()=>{
         err.config.btn.classList.remove("com_loading");
        },200)
    }
    //请求超时
    // if (err.code === 'ECONNABORTED' && err.message.indexOf('timeout') !== -1) {
    //     var config = err.config;
    //     if (!config || !config.retry){
    //         vm.$Message.error("请求超时");
    //         return Promise.reject(err);
    //     } 
    //     config.__retryCount = config.__retryCount || 0;
    //     if (config.__retryCount >= config.retry) {
    //         vm.$Message.error("请求超时");
    //         return Promise.reject(err);
    //     }
    //     config.__retryCount += 1;
    //     var backoff = new Promise(function (resolve) {
    //         setTimeout(function () {
    //             resolve();
    //         }, config.retryDelay || 1);
    //     });
    //     return backoff.then(function () {
    //         return axios(config);
    //     });
    // } else {
        if (err && err.response) {
            switch (err.response.status) {
                case 400:
                     err.message = "错误提示";
                    break;
                case 401:
                    // 401 清除token信息并跳转到登录页面
                    break;
                case 403:
                    err.message = "拒绝访问";
                    break;
                case 404:
                    err.message = "请求错误,未找到该资源";
                    break;
                case 405:
                    err.message = "请求方法未允许";
                    break;
                case 408:
                    err.message = "请求超时";
                    break;
                case 500:
                    err.message = "服务器端出错";
                    break;
                case 501:
                    err.message = "网络未实现";
                    break;
                case 502:
                    err.message = "网络错误";
                    break;
                case 503:
                    err.message = "服务不可用";
                    break;
                case 504:
                    err.message = "网络超时";
                    break;
                case 505:
                    err.message = "http版本不支持该请求";
                    break;
                default:
                    err.message = `链接错误${error.response.status}`;
                    break;
            }
        } else {
            err.message = "网络出现问题,请稍后再试";
        }
        vm.$Message.error(err.message)
        return Promise.reject(err.response.data)
    // }


});

export default axios